package com.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.admin.common.constant.UserConstants;
import com.cloud.admin.common.core.domain.AjaxResult;
import com.cloud.admin.common.core.page.TableDataInfo;
import com.cloud.admin.model.entity.SysPost;
import com.cloud.admin.service.SysPostService;
import com.cloud.admin.shiro.security.ShiroUtils;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * Created on 2019/7/25.
 *
 * @author douhaichao
 */
@RestController
@AllArgsConstructor
@RequestMapping("system/post")
public class PostController extends BaseController {

    private final String prefix = "system/post/";

    private final SysPostService postService;

    @RequiresPermissions("system:post:view")
    @GetMapping()
    public ModelAndView operlog() {
        ModelAndView view = super.baseView("post", prefix + "post");
        return view;
    }

    @RequiresPermissions("system:post:list")
    @PostMapping("list")
    public TableDataInfo list(SysPost post, Page page) {
        IPage pageData = postService.selectPageByLike(page, post);
        return getDataTable(pageData);
    }

    @RequiresPermissions("system:post:remove")
    @PostMapping("remove")
    public AjaxResult remove(String ids) {
        try {
            return toAjax(postService.removeByIds(Arrays.stream(ids.split(",")).collect(Collectors.toList())));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("add")
    public ModelAndView add() {
        ModelAndView view = super.baseView("add", prefix + "add");
        return view;
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:post:add")
    @PostMapping("add")
    public AjaxResult addSave(@Validated SysPost post) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(ShiroUtils.getLoginName());
        post.setCreateTime(new Date());
        return toAjax(postService.save(post));
    }

    /**
     * 修改岗位
     */
    @GetMapping("edit/{postId}")
    public ModelAndView edit(@PathVariable("postId") Long postId, ModelMap mmap) {
        ModelAndView view = super.baseView("edit", prefix + "edit");
        view.addObject("post", postService.getById(postId));
        return view;
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:post:edit")
    @PostMapping("edit")
    public AjaxResult editSave(@Validated SysPost post) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(ShiroUtils.getLoginName());
        post.setUpdateTime(new Date());
        return toAjax(postService.updateById(post));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("checkPostNameUnique")
    public String checkPostNameUnique(SysPost post) {
        return postService.checkPostNameUnique(post);
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("checkPostCodeUnique")
    public String checkPostCodeUnique(SysPost post) {
        return postService.checkPostCodeUnique(post);
    }
}
